function  X = uptrbk(A,B)
% X = uptrbk(A,B)
% A is an nxn non-singular matrix, input.
% B is a  nx1 vector, input.
% X is an nx1 vector, output.
% Partial pivoting is used.
[n n] = size(A);
A = [A';B']';
X = zeros(n,1);
row = 1:n;       % Initialize the pointer vector.
for p = 1:n-1,
  [Max J] = max(abs(A(p:n,p)));  % Find the pivot row.
  T = row(p);
  row(p) = row(J+p-1);
  row(J+p-1) = T;  
  if  A(row(p),p) == 0, break, end
  for k = p+1:n,
    M = A(row(k),p)/A(row(p),p);
    A(row(k),p+1:n+1) = A(row(k),p+1:n+1) - M*A(row(p),p+1:n+1);
  end
end
X(n) = A(row(n),n+1)/A(row(n),n);
for k = n-1:-1:1,
  X(k) = (A(row(k),n+1) - A(row(k),k+1:n)*X(k+1:n))/A(row(k),k);
end
